﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using DTO;
using DataService;

public partial class UserControls_ContactControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlTypeBilling.DataSource = (new ContactService()).GetContactTypes();
            DataBind();
        }
    }

    public Contact Contact
    {
        set
        {
            ViewState["ContactId"] = value.Id;
            txtFirstName.Text = value.FirstName;
            txtLastName.Text = value.LastName;
            txtEmail.Text = value.Email;
            txtPhone.Text = value.Phone;
            ddlTypeBilling.SelectedValue = value.Type.Id.ToString();
        }
        get
        {
            int id = Convert.ToInt32(ViewState["ContactId"]);
            return new Contact(id)
            {
                FirstName = txtFirstName.Text.Trim(),
                LastName = txtLastName.Text.Trim(),
                Email = txtEmail.Text.Trim(),
                Phone = txtPhone.Text.Trim(),
                Type = new Contact.ContactType(Convert.ToInt32(ddlTypeBilling.SelectedValue))
                {
                    Name = ddlTypeBilling.Text
                }
            };
        }
    }
}